<?php
class News_model extends CI_Model
{
	public function getBestNews($lang='en')
	{
		$data = array();
		$this->db->select('n.createdDate , n.author ,nl.news_name');
		$this->db->from('news n');
		$this->db->where('n.public','1');
		$this->db->join('news_languages nl','n.id = nl.news_id');
		$this->db->join ( 'languages l', 'l.id = nl.language_id AND l.code =\'' . $lang . '\'' );
		$q = $this->db->get();
		if($q->num_rows())
		{
			$data = $q->result_array();
		}
		$q->free_result();
		return $data;
	}
	public function getNewsContentByNewsName($name,$lang)
	{
		$data = array();
		$this->db->select('nl.news_content');
		$this->db->from('news n');
		$this->db->where('n.public','1');
		$this->db->where('nl.news_name',$name);
		$this->db->join('news_languages nl','n.id = nl.news_id');
		$this->db->join ( 'languages l', 'l.id = nl.language_id AND l.code =\'' . $lang . '\'' );
		$q = $this->db->get('news_languages');
		if($q->num_rows())
		{
			$data = $q->row_array();
		}
		$q->free_result();
		return $data;
	}
	public function getall()
	{
		$data = array();
		$q = $this->db->get('news_languages');
		if($q -> num_rows()){
		$data = $q->result_array();
		}
		$q -> free_result();
		return $data;
	}
public function insertNews()
	{
		$data = array(
			'author' => $_POST['author'],
			'createdDate' => now(),
		 	'public' => '1',
			'order' => $_POST['order']
		);
		$this->db->insert('news',$data);
		
		$this->db->select('max(id)');
		$this->db->from('news');
		$q = $this->db->get();
		if($q->num_rows())
		{
			$data = $q->row_array();
		}
		$data = array(
			'news_id' =>$data['max(id)'],
			'language_id' => $_POST['lang'],
			'news_name'=>$_POST['news_name'],
			'news_content' => $_POST['MyTextarea']
		);
		$this->db->insert('news_languages',$data);
	}

}